package com.test;

import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class test {
	
	public static void getFruitInfo(Class<?> clazz){
        
        String strFruitName=" 水果名称：";
        String strFruitColor=" 水果颜色：";
        String strFruitProvicer="供应商信息：";
        
        Field[] fields = clazz.getDeclaredFields();
        
        for(Field field :fields){
            if(field.isAnnotationPresent(FruitName.class)){
                FruitName fruitName = (FruitName) field.getAnnotation(FruitName.class);
                strFruitName=strFruitName+fruitName.value();
                System.out.println(strFruitName);
            }
//            else if(field.isAnnotationPresent(FruitColor.class)){
//                FruitColor fruitColor= (FruitColor) field.getAnnotation(FruitColor.class);
//                strFruitColor=strFruitColor+fruitColor.fruitColor().toString();
//                System.out.println(strFruitColor);
//            }
//            else if(field.isAnnotationPresent(FruitProvider.class)){
//                FruitProvider fruitProvider= (FruitProvider) field.getAnnotation(FruitProvider.class);
//                strFruitProvicer=" 供应商编号："+fruitProvider.id()+" 供应商名称："+fruitProvider.name()+" 供应商地址："+fruitProvider.address();
//                System.out.println(strFruitProvicer);
//            }
        }
    }

	public static void main(String[] args) throws ClassNotFoundException {
		// TODO Auto-generated method stub
		System.out.println("111111");
		getFruitInfo(Apple.class);
		
		//包名
        String basePack = "com";
        //先把包名转换为路径,首先得到项目的classpath
        String classpath = test.class.getResource("/").getPath();
        //然后把我们的包名basPach转换为路径名
        basePack = basePack.replace(".", File.separator);
        //然后把classpath和basePack合并
        String searchPath = classpath + basePack;
        doPath(new File(searchPath));
        
        for (String s : classPaths) {
            s = s.replace(classpath.replace("/","\\").replaceFirst("\\\\",""),"").replace("\\",".").replace(".class","");
//        	System.out.println(s);
            Class cls = Class.forName(s);
            System.out.println(cls);
            Field[] fields = cls.getDeclaredFields();
            for(Field field : fields) {
            	System.out.println(field);
            }
            Annotation[] ann = cls.getDeclaredAnnotations();
            for(Annotation f : ann) {
            	System.out.println(f);
            }
            Constructor[] a = cls.getDeclaredConstructors();
            for(Constructor f : a) {
            	System.out.println(f.toGenericString());
            }
            System.out.println();
        }
        
        System.out.println(searchPath);
	}
	static List<String> classPaths = new ArrayList<String>();
	
	/**
     * 该方法会得到所有的类，将类的绝对路径写入到classPaths中
     * @param file
     */
    private static void doPath(File file) {
        if (file.isDirectory()) {//文件夹
            //文件夹我们就递归
            File[] files = file.listFiles();
            for (File f1 : files) {
                doPath(f1);
            }
        } else {//标准文件
            //标准文件我们就判断是否是class文件
            if (file.getName().endsWith(".class")) {
                //如果是class文件我们就放入我们的集合中。
                classPaths.add(file.getPath());
            }
        }
    }

}
